<?php
namespace Admin\Controller;
use Think\Controller;
/**
* 活动控制器
*/
class ActivityController extends CommonController{
    //活动列表
	public function index(){
        $page_no = I('get.p') ? I('get.p') : 1;
        $page_size = $_GET['page_size'] ? $_GET['page_size'] : 10;
        session('page_size',$page_size);
        $MODEL = M("activity a");
        $where['del'] = '0';
        if(isset($_GET['theme']) && is_numeric($_GET['theme']) && $_GET['theme'] != 0){
            $where['theme'] = $_GET['theme'];
        }
        if($_GET['username']){
            if(is_numeric($_GET['username'])){
                $where['_string'] = 'b.id=' . $_GET['username'] . ' OR b.phone=' . $_GET['username'] . ' OR b.username='.$_GET['username'];
            }else{
                $where['b.username'] = array('like','%' . $_GET['username'] . '%');
            }
        }
        if(isset($_GET['city']) && !empty($_GET['city'])){
            $where['a.city'] = array('like', $_GET['city'].'%');
        }

        $data = $MODEL
                ->field("a.id,a.city,a.cate,a.theme,a.site,a.start_time,a.bao_ed,a.join_ed,a.status,b.username")
                ->page($page_no, $page_size)
                ->join('left join zm_user b on a.user_id=b.id')
                ->order('id desc')->where($where)->select();

        $count = M("activity a")->join('left join zm_user b on a.user_id=b.id')->where($where)->count();

        foreach ($data as $key => $value) {
            if($value['theme'] == 1){
                $data[$key]['theme'] = '美食';
            }elseif ($value['theme'] == 2) {
                $data[$key]['theme'] = 'K歌';
            }else{
                $data[$key]['theme'] = '酒吧';
            }
            if($value['cate'] == 1){
                $data[$key]['cate'] = '约会';
            }else{
                $data[$key]['cate'] = '聚会';
            }
            $data[$key]['str_status'] = $this->getFaqiStrStatus($value);
        }
        $Page = new \AntAge\Page($count, $page_size);
        $show = $Page->show();
        $this->assign('page', $show);
        $this->assign("count", $count);
        $this->assign("data", $data);
        $this->display();
	}


    //活动详情
    public function detail(){
        $id = I('get.id');
        if(!$id){
            $this->error('参数错误');
        }
        $data = M("activity a")
            ->field("a.id,a.cate,a.theme,a.site,a.start_time,a.bao_ed,a.join_ed,a.status,b.username")
            ->join('left join zm_user b on a.user_id=b.id')
            ->where('a.id='.$id)
            ->find();
        $data['str_status'] = $this->getFaqiStrStatus($data);
        
        $joiners = M('activity_log a')
                ->field('a.id,a.user_id,a.add_time,a.is_invited,a.is_arrive,a.is_gift,a.status,a.lat,a.lng,a.is_remark,a.del,b.username,b.sex,b.age')
                ->join('left join zm_user b on a.user_id=b.id')
                ->where("(a.activity_id=".$id." AND a.is_invited > 0) OR (a.activity_id=".$id." AND a.status=12)")
                ->select();
        foreach ($joiners as $key => $value) {
            $joiners[$key]['str_status'] = $this->getjoinStrStatus($value,$data);
        }
        //活动类别
        if($data['cate'] == 1){
            $data['cate'] = '约会';
        }else{
            $data['cate'] = '聚会';
        }
        //活动主题
        if($data['theme'] == 1){
            $data['theme'] = '美食';
        }elseif ($data['theme'] == 2) {
            $data['theme'] = 'K歌';
        }else{
            $data['theme'] = '酒吧';
        }
        $this->assign('data',$data);
        $this->assign('joiners',$joiners);
        $this->display();
    }
    public function join(){
        $page_no = I('get.p') ? I('get.p') : 1;
        $page_size = $_GET['page_size'] ? $_GET['page_size'] : 10;
        $user_id = $_GET['user_id'];
        if(!$user_id){
            $this->error('参数错误');
        }
        $where['a.is_invited'] = array('gt',0);
        $where['a.user_id'] = $user_id;
        $data = M('activity_log a')->field('a.add_time,a.is_invited,a.is_gift,a.status as astatus,a.is_arrive,a.is_remark,b.start_time,b.site,b.cate,b.theme,b.id,b.bao_ed,b.join_ed,b.status as bstatus,c.username')
            ->page($page_no, $page_size)
            ->join('left join zm_activity b on a.activity_id=b.id')
            ->join('left join zm_user c on b.user_id=c.id')
            ->where($where)
            ->select();
    
        $count = M("activity_log a")->where($where)->count();
        foreach ($data as $key => $value) {
            if($value['theme'] == 1){
                $data[$key]['theme'] = '美食';
            }elseif ($value['theme'] == 2) {
                $data[$key]['theme'] = 'K歌';
            }else{
                $data[$key]['theme'] = '酒吧';
            }
            $data[$key]['str_status'] = getActiviStrStatus($value);
        }
        $Page = new \AntAge\Page($count, $page_size);
        $show = $Page->show();
        $this->assign('page', $show);
        $this->assign("count", $count);
        $this->assign("data", $data);
        $this->display();
    }

    public function faqi(){
        $page_no = I('get.p') ? I('get.p') : 1;
        $page_size = $_GET['page_size'] ? $_GET['page_size'] : 10;
        $MODEL = M("activity a");
        $user_id = $_GET['user_id'];
        if(!$user_id){
            $this->error('参数错误');
        }
        $where['a.user_id'] = $user_id;
        $where['a.del'] = '0';
        $data = M("activity a")
                ->field("a.id,a.cate,a.theme,a.site,a.start_time,a.bao_ed,a.join_ed,a.status,b.username")
                ->page($page_no, $page_size)
                ->join('left join zm_user b on a.user_id=b.id')
                ->order('id desc')->where($where)->select();
        $count = M("activity a")->where($where)->count();

        foreach ($data as $key => $value) {
            if($value['theme'] == 1){
                $data[$key]['theme'] = '美食';
            }elseif ($value['theme'] == 2) {
                $data[$key]['theme'] = 'K歌';
            }else{
                $data[$key]['theme'] = '酒吧';
            }
            $data[$key]['str_status'] = getActiviStrStatus($value);
        }
        $Page = new \AntAge\Page($count, $page_size);
        $show = $Page->show();
        $this->assign('page', $show);
        $this->assign("count", $count);
        $this->assign("data", $data);
        $this->display();
    }

    public function getFaqiStrStatus($activity){
        $now = time();
        if($activity['start_time'] > $now){
            if($activity['join_ed'] > 0){
                $str_status = '待签到';
            }elseif($activity['bao_ed'] > 0){
                $str_status = '未邀约';
            }else{
                $str_status = '等待报名中';
            }
        }elseif($activity['start_time'] > ($now-90*60)){
            //活动开始时间是8点，则8点到9点半之间都可以
            if($activity['status'] == 1){
                $str_status = '已取消';
            }elseif($activity['status'] == 9){
                $str_status = '已完成';
            }elseif($activity['is_arrive'] == 1){
                $str_status = '已签到';
            }elseif($activity['join_ed'] > 0){
                $str_status = '待签到';
            }else{
                $str_status = '已过期';
            }
        }else{
            if($activity['status'] == 11){
                $str_status = '爽约';
            }elseif($activity['status'] == 9){
                $str_status = '完成';
            }elseif ($activity['status'] == 13) {
                $log = M('activity_log')->where(array('activity_id'=>$activity['id'],'is_invited'=>2))->find();
                if($log['status'] == 18){
                    $str_status = '未完成';
                }else{
                     $str_status = '等待参与人签到';
                }
               
            }elseif ($activity['status'] == 1) {
                $str_status = '已取消';
            }elseif ($activity['status'] == 7) {
                if($activity['join_ed'] > 0)
                    $str_status = '爽约';
            }else{
                $str_status = '过期';
            }
        }
        return $str_status;
    }

    public function getjoinStrStatus($acLog,$activity){
        $now = time();
        if($activity['start_time'] > $now){
            if($acLog['is_invited'] == 1){
                if($activity['join_ed'] == 0){
                    $str_status = '待邀约';
                }else{
                    $str_status = '未被邀约';
                }
            }
            if($acLog['is_invited'] == 2){
                $str_status = '已被邀约';
            }
        }elseif($activity['start_time'] > ($now-90*60)){
            //活动开始时间是8点，则8点到9点半之间都可以
            if($acLog['status'] == 16){
                $str_status = '已完成';
            }elseif($acLog['status'] == 14){
                if($acLog['is_gift'] == 1){
                    $str_status = '已领见面礼';
                }elseif ($acLog['is_arrive'] == 1) {
                    $str_status = '已签到';
                }else{
                    $str_status = '待签到';
                }
            }elseif($acLog['status'] == 11){
                if($activity['join_ed'] == 0){
                    $str_status = '过期';
                }else{
                    $str_status = '未被邀约';
                }
            }else{
                $str_status = '过期';
            }
        }else{
            if($acLog['status'] == 18){
                $str_status = '爽约';
            }elseif($acLog['status'] == 16){
                $str_status = '已完成';
            }elseif ($acLog['status'] == 14) {
                if($acLog['is_gift'] == 1){
                    $str_status = '已领见面礼';
                }else{
                    $str_status = '未领见面礼';
                }
            }else{
                if($activity['status'] == 1){
                    $str_status = '已取消';
                }else{
                    $str_status = '过期';
                }
            }
        }
        return $str_status;
    }
}